package com.example.com.skills_utf8;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;

import com.app.G;
import com.app.util.*;


//
// 将bean放在里面呢
public class _62DB_Message implements BaseColumns{
	
	
	public static final String TABLE_NAME = "_62DB_Message"; //_62DB_Message.this.getClass().getSimpleName(); //"download_t";
	public static final Uri CONTENT_URI = Uri.parse("content://"+
			G.PACK_NAME+"/"+TABLE_NAME);

	public static final Class<?> cls = Bean.class;
	private static List<String> mFieldArray  = new ArrayList<String>();
	

	// 创建表  这种写法促使了任意数据结构只能有一张表.   以数据结构为对象
	public static void createTable(SQLiteDatabase db)
	{

		Field[] field_array = cls.getDeclaredFields();
		String str =""; 
		for(int i =0; i<field_array.length; i++){
			Field f = field_array[i];          
			String name = f.getName();
			str += name + "  TEXT,"; 
			mFieldArray.add(name);
		}



		str = str.substring(0, str.length()-1); //
		
		String tableCreateSql = 
				"CREATE TABLE "+TABLE_NAME+" (" +
				_ID+" INTEGER PRIMARY KEY," +
				str +
				");";
				;
		

	       	try {
	    		db.execSQL(tableCreateSql);
	    	} catch(SQLException e) {
	    		e.printStackTrace();
	    	}
	}


	
	public static List<Bean> GetTable(Context activity ){
		
		List<Bean> track_List = new ArrayList<Bean>();
		
		StringBuffer count = new StringBuffer("");
		SQLiteOpenHelper mOpenHelper = DatabaseHelper
				.getInstance(activity);
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		
		try {
			
			{
				String[] all_where = new String[1];
				String  sql = "select * from "+ TABLE_NAME + " ";
				cursor = db.rawQuery(sql, null);
			}
			
			if(cursor!=null&&cursor.moveToFirst())
			{
				int i = cursor.getCount();
				for(int j = 0;j<i;j++)
				{
					
					// 不用考虑具体对象的内容,拿到对应数据
					//Bean tm = new Bean();
					Object obj = cls.newInstance();

					Method[] methods = cls.getMethods();
					// 遍历对象的方法
					for (Method method : methods) {
						String methodName = method.getName();
						// 如果对象的方法以set开头
						if (methodName.startsWith("set")) {
							// 根据方法名字得到数据表格中字段的名字
							String columnName = methodName.substring(3,
									methodName.length());
							// 得到方法的参数类型
							Class[] parmts = method.getParameterTypes();
							if (parmts[0] == String.class) {
								// 如果参数为String类型，则从结果集中按照列名取得对应的值，并且执行改set方法

								String str = cursor.getString( cursor.getColumnIndex(columnName) );
								method.invoke(obj, str);
							}
							if (parmts[0] == int.class) {

								int result = cursor.getInt( cursor.getColumnIndex(columnName) );
								method.invoke(obj, result);
							}
						}

					}


					//tm.setContent(cursor.getString( cursor.getColumnIndex("content") ));
					//tm.setMd5(cursor.getString(  cursor.getColumnIndex("md5")  ));
					
					/*
					for(int k =0; k<cursor.getColumnCount(); k++)
					{
						String a1 = cursor.getString(k);
						tm.setContent(a1);   //......注意这里写死了... 没有看到反射提供修改值
					}
					*/
					
					track_List.add((Bean)obj);
					cursor.moveToNext();
			
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cursor != null) cursor.close();
		}
		
		
		return track_List;
		
		
	}


	public static void Save(Activity activity){

		SQLiteOpenHelper mOpenHelper = DatabaseHelper
				.getInstance(activity);
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		Cursor cursor = null;



	}



	// 通过了ContendProvider..
	public static void SaveBaseMessageToDb(ContentResolver cr, Bean tm)
	{
		if(cr==null||tm==null)
		{
			return ;
		}
		Cursor cursor = null;
		ContentValues bm = new ContentValues();
		
		// if this operator contain .......
		String Where = null;
//				mFieldArray.get(0) +
//				" = "+"'"+tm.getContent()+"'"
//				;  
		


		// 设置这里没有满足
		//String projection = mFieldArray.toString();
		//Object[] tt = mFieldArray.toArray();
		String[] projection = mFieldArray.toArray(new String[mFieldArray.size()]);
		
		bm.put(mFieldArray.get(0), tm.getContent());
		bm.put(mFieldArray.get(1), tm.getMd5());
		//bm.put(_62DB_Message.str_url, tm.url);
		
		cursor = cr.query(CONTENT_URI, 
				projection, Where, null, null);
		if(cursor!=null 
				&&
				cursor.moveToFirst()
				&&
				0!=cursor.getInt(0))   // 
		{
			
			/*
			// 更新
			cr.update(CONTENT_URI, bm, Where, null);
			System.out.println("update");
			*/
			cr.insert(CONTENT_URI, bm);
			System.out.println("inster");
			
		}
		else // 插入
		{
	
			cr.insert(CONTENT_URI, bm);
			System.out.println("inster");
		}
		cursor.close();
	}




	public static void remove(Context context, String str){
		
		StringBuffer count = new StringBuffer("");
		SQLiteOpenHelper mOpenHelper = DatabaseHelper
				.getInstance(context);
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		
		{
			String[] all_where = new String[1];
			String  sql = "delete from "+ _62DB_Message.TABLE_NAME + " where " +
					"md5" + "='" + str + "'";  
			db.execSQL(sql);
		}
		

		
	}
	



/////////////////////////////////////////////////////////////////
	public static class Bean
	{
		private String content;
		private String md5;
	//	private String bytes;
		
		
		public String getContent() {
			return content;
		}
		public void setContent(String content) {
			this.content = content;
		}
		public String getMd5() {
			return md5;
		}
		public void setMd5(String md5) {
			this.md5 = md5;
		}
//		public String getBytes() {
//			return bytes;
//		}
//		public void setBytes(String bytes) {
//			this.bytes = bytes;
//		}
		
	}
	
	
	
}

